﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SingleResultInteger64.cs" company="DHGMS Solutions">
//   Copyright 2004-2015 DHGMS Solutions
//   
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//   
//       http://www.apache.org/licenses/LICENSE-2.0
//   
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// <summary>
//   Represents a task that returns a single integer 64 result
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.Info.TaskResult
{
        using System;
        using System.ComponentModel.DataAnnotations;
        using System.Diagnostics;
        using System.Diagnostics.CodeAnalysis;
        using System.Runtime.Serialization;
        using System.Xml;
        using System.Xml.Linq;

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Represents a task that returns a single integer 64 result
        /// </summary>
        [DataContract]
        public class SingleResultInteger64
// ReSharper disable RedundantNameQualifier
            : TaskResultBase
// ReSharper restore RedundantNameQualifier
        {
        #region fields
            /// <summary>
            /// The result for the task
            /// </summary>
            [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1121:UseBuiltInTypeAlias", Justification = "Reviewed. Suppression is OK here.")]
            private long? result;

        #endregion

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Initializes a new instance of the <see cref="SingleResultInteger64"/> class.
        /// </summary>
        public SingleResultInteger64()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SingleResultInteger64"/> class.
        /// </summary>
        /// <param name="other">
        /// Object to copy
        /// </param>
        public SingleResultInteger64(SingleResultInteger64 other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            this.Result = other.Result;
        }

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Initializes a new instance of the <see cref="SingleResultInteger64"/> class.
        /// </summary>
        /// <param name="name">Name of the task</param>
        /// <param name="started">Timestamp of when the task started</param>
        /// <param name="finished">Timestamp of when the task finished</param>
        /// <param name="result">The result for the task</param>
        public SingleResultInteger64(
            string name,            DateTime started,            DateTime finished,            long? result)
            : base(
                name,
                started,
                finished)
            {
            this.Result = result;
        }

        #region properties
        /// <summary>
        /// Gets or sets The result for the task
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1121:UseBuiltInTypeAlias", Justification = "Reviewed. Suppression is OK here.")]
        [DataMember(IsRequired = false, Order = 1)]
[Range(typeof(long), "0", "")]
        public long? Result
        {
            get
            {
                return this.result;
            }

            set
        {
            if(value != null && value < 0)
            {
                // ReSharper disable RedundantNameQualifier
                throw new Dhgms.DataManager.Model.Exception.NumberTooLowInteger64Exception("Result",0, value.Value);
                // ReSharper restore RedundantNameQualifier
            }
            this.result = value;
        }
        }

            /// <summary>
            /// Gets a header record for use for something like a CSV file
            /// </summary>
            /// <returns>a collection of strings representing the header record</returns>
            public override System.Collections.Generic.IList<string> HeaderRecord
            {
                get
                {
                    var result = new System.Collections.Generic.List<string>(base.HeaderRecord)
                    {
                        "Result"
                    };

                    return result;
                }
            }

        #endregion

        #region IComparable methods

        // ********** WARNING **********
        // This code is automatically generated! Any Changes you make to this file will be lost!
        // To make changes edit the corresponding .tt file!

        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="other">
        /// The instance to compare to
        /// </param>
        /// <returns>
        /// 0 if equal, otherwise non zero
        /// </returns>
        public int CompareTo(SingleResultInteger64 other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }


        var checkResult = base.CompareTo(other);
        if (checkResult != 0)
        {
            return checkResult;
        }

            // Result
            if (this.Result != null)
            {
                if (other.Result == null)
                {
                    checkResult = -1;
                }
                else
                {
                    checkResult = this.Result.Value.CompareTo(other.Result.Value);
                }
            }
            else if (other.Result != null)
            {
                checkResult = 1;
            }

            if (checkResult != 0)
            {
                return (checkResult > 0) ? 4 : -4;
            }

            return 0;
        }

        #endregion
#region IEquatable methods
            /// <summary>
            /// Checks if the current instance matches another of the same type
            /// </summary>
            /// <param name="other">object to compare</param>
            /// <returns>true if equal, otherwise false</returns>
            public bool Equals(SingleResultInteger64 other)
            {
                return this.CompareTo(other) == 0;
            }

#endregion

    #region our methods

        /// <summary>
        /// Gets the hash code for the object
        /// </summary>
        /// <returns>hash code</returns>
        public override int GetHashCode()
        {
            return
                base.GetHashCode() ^
                this.Result.GetHashCode()
                ^ this.Result.GetHashCode();
        }

        /// <summary>
        /// Checks a table to ensure it meets the required schema
        /// </summary>
        public override void DoTableValidation()
        {
        }

            /// <summary>
            /// Gets a collection of string data for use for something like a CSV file
            /// </summary>
            /// <returns>a collection of strings representing the data record</returns>
            public override System.Collections.Generic.IList<string> ToStringArray()
            {
                var result = new System.Collections.Generic.List<string>(base.ToStringArray())

                {
                    this.Result.HasValue ? Result.Value.ToString(System.Globalization.CultureInfo.InvariantCulture) : null
                };

                return result;
            }

            /// <summary>
            /// Adds an XML Element to an XML Writer
            /// </summary>
            /// <param name="writer">
            /// The XML writer to add the element to.
            /// </param>
            /// <param name="parentElementName">
            /// The name for the parenet element being produced.
            /// </param>
            public override void DoXmlElement(
                    System.Xml.XmlWriter writer,
                    string parentElementName)
            {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (string.IsNullOrEmpty(parentElementName) || parentElementName.Trim().Length == 0)
            {
            throw new ArgumentNullException("parentElementName");
            }

                writer.WriteStartElement(parentElementName);

                // Name
                this.DoChildXmlCDataElement(writer, "Name", this.Name);

                // Started
                this.DoChildXmlCDataElement(writer, "Started", this.Started.ToString("yyyy-MM-dd HH:mm:ss"));

                // Finished
                this.DoChildXmlCDataElement(writer, "Finished", this.Finished.ToString("yyyy-MM-dd HH:mm:ss"));

                // Result
                this.DoChildXmlCDataElement(writer, "Result", this.Result.HasValue ? Result.Value.ToString(System.Globalization.CultureInfo.InvariantCulture) : null);

                writer.WriteEndElement();
            }

        /// <summary>
        /// Checks this instance against another to see where there are differences
        /// </summary>
        /// <param name="other">other instance to compare</param>
        /// <returns>summary of where there are differences</returns>
// ReSharper disable RedundantNameQualifier
        public Dhgms.TplHelper.Model.Difference.TaskResult.SingleResultInteger64Difference GetDifferences(SingleResultInteger64 other)
// ReSharper restore RedundantNameQualifier
        {
            if (other == null)
            {
                throw new System.ArgumentNullException("other");
            }

            // Name
            var checkResult = string.CompareOrdinal(this.Name, other.Name);

            var nameDifferent = checkResult != 0;

            // Started
            checkResult = this.Started.CompareTo(other.Started);

            var startedDifferent = checkResult != 0;

            // Finished
            checkResult = this.Finished.CompareTo(other.Finished);

            var finishedDifferent = checkResult != 0;

            // Result
            if (this.Result != null)
            {
                if (other.Result == null)
                {
                    checkResult = -1;
                }
                else
                {
                    checkResult = this.Result.Value.CompareTo(other.Result.Value);
                }
            }
            else if (other.Result != null)
            {
                checkResult = 1;
            }

            var resultDifferent = checkResult != 0;

// ReSharper disable RedundantNameQualifier
            return new Dhgms.TplHelper.Model.Difference.TaskResult.SingleResultInteger64Difference(
// ReSharper restore RedundantNameQualifier
                nameDifferent,
                startedDifferent,
                finishedDifferent,
                resultDifferent);
        }

        /// <summary>
        /// Gets the CDSL that defines the OData Vocabularies for this class
        /// </summary>
        public static new XmlReader GetOdataVocabularies()
        {
            // Result

        var schema = new XElement(
            "Schema",
            new XAttribute("Namespace", "Dhgms.TplHelper.TaskResult"),
            new XAttribute("xmlns", "http://schemas.microsoft.com/ado/2009/11/edm"),
            // using directive
            new XElement(
                "Using",
                new XAttribute("Namespace", "Org.OData.Validation.V1"),
                new XAttribute("Alias", "Validation"),
                new XElement("Annotations",
                    new XAttribute("Target", "Dhgms.TplHelper.TaskResult.SingleResultInteger64/Name")),
                new XElement("Annotations",
                    new XAttribute("Target", "Dhgms.TplHelper.TaskResult.SingleResultInteger64/Started")),
                new XElement(
                    "Annotations",
                    new XAttribute("Target", "Dhgms.TplHelper.TaskResult.SingleResultInteger64/Finished")),
                new XElement(
                    "Annotations",
                    new XAttribute("Target", "Dhgms.TplHelper.TaskResult.SingleResultInteger64/Result"))));

        Debug.Assert(schema.Document != null, "schema.Document != null");
        return schema.Document.CreateReader();
        }
        #endregion
            /// <summary>
            /// The on disposing event
            /// </summary>
            protected override void OnDisposing()
            {
                base.OnDisposing();
            this.result = null;
            }
    }
}
